package com.fdb.efp.nls.service.impl;

import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.basic.framework.core.constant.BaseConstant;
import com.fdb.basic.framework.core.util.DataFileUtil;
import com.fdb.efp.nls.service.dao.BatComaccSsDao;
import com.fdb.efp.nls.service.domain.BatComaccSs;
import com.fdb.efp.nls.service.facade.BatComaccSsService;
import com.fdb.efp.nls.service.vo.BatComaccSsVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>ServiceImpl: 贷款实收中间表				<p>
 * @author chenqm1
 * @date 2018-06-05
 */
@Service("batComaccSsService")
public class BatComaccSsServiceImpl implements BatComaccSsService, FrameworkService {
	
	private static Logger logger = LoggerFactory.getLogger(BatComaccSsServiceImpl.class);

	@Autowired
	private BatComaccSsDao batComaccSsDao;
	
	/**
	 * 新增操作
	 */
	public int insertBatComaccSs(BatComaccSsVO inBatComaccSsVo) {
		logger.debug("当前新增数据为:"+ inBatComaccSsVo.toString());
		int num = 0;
		try {
			BatComaccSs batComaccSs = new BatComaccSs();
			beanCopy(inBatComaccSsVo, batComaccSs);
			num = batComaccSsDao.insertBatComaccSs(batComaccSs);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteAll(BatComaccSsVO inBatComaccSsVo) {
		logger.debug("当前删除数据条件为:"+ inBatComaccSsVo);
		int num = 0;
		try {
			BatComaccSs batComaccSs = new BatComaccSs();
			beanCopy(inBatComaccSsVo, batComaccSs);
			num = batComaccSsDao.deleteAll(batComaccSs);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inBatComaccSsVo +"删除的数据条数为"+ num);
		return num;
	}

	@Override
	public boolean fileDataInsertTable(File file, BatComaccSsVO clazz, String operation) {
		boolean result = true;
		try {
			List<BatComaccSsVO> readTxt = DataFileUtil.readTxt(file,clazz.getClass(),"batComaccSs.json","GB2312");
			if(readTxt != null && readTxt.size() > 0) {
				int sum = readTxt.size();
				logger.info("文件中总数据量为:"+sum);
				int batchNum = BaseConstant.batchInsertNum_5000;
				logger.info("分批处理设置每次处理条数:"+batchNum);
				int cycleNum = sum % batchNum == 0 ? sum/batchNum : sum/batchNum +1;
				logger.info("需要执行的处理次数:" + cycleNum);
				int insertNum = 0;
				for(int i=0;i<cycleNum;i++) {
					logger.info("开始执行第"+(i+1)+"次处理");
					int start = i*batchNum; 
					int end = start + batchNum;
					logger.info("查询数据范围:"+(start+1)+"~"+end);
					List<BatComaccSsVO> subList = new ArrayList<>();
					if(end > readTxt.size()) {
						subList = readTxt.subList(start, readTxt.size());
					}else {
						subList = readTxt.subList(start, end);
					}
					
					List<BatComaccSs> beanCopy = (List<BatComaccSs>)beansCopy(subList, BatComaccSs.class);
					int num = batComaccSsDao.batchInsertBatComaccSs(beanCopy);
					insertNum = insertNum + num;
					
				}
				logger.info("文件中数据成功插入数量为:" + insertNum);
			}
		} catch (Exception e) {
			result = false;
			logger.error("文件数据插入表过程出错", e);
		}
		return result;
	}
	
}
